package com.timss.opmm.util;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import com.timss.opmm.bean.Duty;

/**
 * 
 * @title: VOUtil
 * @description: json转vo , vo 转JSON
 * @company: gdyd
 * @className: VOUtil.java
 * @author: fengzt
 * @createDate: 2014年6月4日
 * @updateUser: fengzt
 * @version: 1.0
 */
public class VOUtil {

    /**
     * 
     * @description:vo转json
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param vo
     * @return:json
     */
    public static String fromVoToJsonUtil(Object vo) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility( JsonMethod.FIELD, org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.ANY );
        mapper.configure( SerializationConfig.Feature.INDENT_OUTPUT, true );
        String json = null;
        try {
            // 页面VO 转JSON
            json = mapper.writeValueAsString( vo );
            //json = URLEncoder.encode( json, "utf-8" );
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }
    
    /**
     * 
     * @description:json转vo
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param dataJson
     * @param clss
     * @return:
     */
    public static <T> T fromJsonToVoUtil(String dataJson , Class<T> clss ) {
        ObjectMapper objectMapper = new ObjectMapper();
        T vo = null;
        try {
            // 1.获取页面参数，解决乱码，tomcat默认是按iso-8859-1的编码传输
            dataJson = URLDecoder.decode( dataJson, "utf-8" );
            dataJson = new String( dataJson.getBytes( "ISO-8859-1" ), "utf-8" );
            vo  = objectMapper.readValue( dataJson, clss );
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vo;
    }
   
    /**
     * 
     * @description:json 转 HashMap
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param dataJson
     * @return:
     */
    public static HashMap<String, Object> fromJsonToHashMap( String dataJson ){
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Object> vo = new HashMap<String, Object>();
        try {
            // 1.获取页面参数，解决乱码，tomcat默认是按iso-8859-1的编码传输
            dataJson = URLDecoder.decode( dataJson, "utf-8" );
            vo  = objectMapper.readValue( dataJson, HashMap.class );
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vo;
    }
    
    
    public static void main(String[] args) {
        Duty duty = new Duty();
        duty.setDeptId(1);
        duty.setId( 1 );
        duty.setName("test2" + new Date().toString() );
        duty.setNum("中文");
        duty.setSiteId(1);
        duty.setSortType(0);
        String json = VOUtil.fromVoToJsonUtil(duty);
        Duty du = VOUtil.fromJsonToVoUtil(json, Duty.class );
        HashMap<String, Object> ob = VOUtil.fromJsonToHashMap(json);
        System.out.println( ob.get("num") + "---------");
        System.out.println( du.getDeptId() );
        System.out.println( json.toString() );
        
        System.out.println( StringUtils.isNotBlank( null ));
    }
}
